﻿Imports System.Data.OleDb
Public Class KhachHangDAO
    Public Function GetKhachHang() As DataTable
        Dim sql As String = "select * from KhachHang"
        Dim dt As New DataTable
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim da As New OleDbDataAdapter(sql, cn)
        da.Fill(dt)
        cn.Close()
        Return dt
    End Function

    Public Sub AddKhachHang(ByVal newKH As KhachHangDTO)
        Dim sql As String = String.Format("insert into KhachHang(MaKH,TenKH,NgaySinh,GioiTinh,SDT,DiaChi,TheVip,CMND,NgayCap,MaNV) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", _
                                            newKH.MaKH, newKH.TenKH, newKH.NgaySinh, newKH.GioiTinh, newKH.SDT, newKH.DiaChi, newKH.TheVip, newKH.CMND, newKH.NgayCap, newKH.MaNV)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Vui lòng kiểu tra lại dữ liệu !")
        End Try

        cn.Close()
    End Sub

    Public Sub DelKhachHang(ByVal delKH As KhachHangDTO)
        Dim sql As String = String.Format("delete from KhachHang where STT={0}", delKH.STT)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub

    Public Sub UpdateKhachHang(ByVal newKH As KhachHangDTO)
        Dim sql As String = String.Format("update KhachHang set MaKH='{0}',TenKH='{1}',NgaySinh='{2}',GioiTinh='{3}',SDT='{4}',DiaChi='{5}',TheVip='{6}',CMND='{7}',NgayCap='{8}',,MaNV='{9}', where STT={10}", _
                                           newKH.MaKH, newKH.TenKH, newKH.NgaySinh, newKH.GioiTinh, newKH.SDT, newKH.DiaChi, newKH.TheVip, newKH.CMND, newKH.NgayCap, newKH.MaNV, newKH.STT)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Vui lòng kiểu tra lại dữ liệu !")
        End Try
        cn.Close()
    End Sub
End Class
